﻿var mainViewModel = new MainViewModel();

function MainViewModel() {
    var selfMain = this;

    selfMain.CurrentPage = ko.observable();

    selfMain.IsValidModel = function (model) {
        for (var property in model) {
            if (model[property].hasError)
                if (model[property].hasError())
                    return false;
        }
        return true;
    }

    ko.extenders.required = function (target, overrideMessage) {
        target.hasError = ko.observable();
        target.validationMessage = ko.observable();

        function validate(newValue) {
            target.hasError(whoever.IsEmpty(newValue));
            target.validationMessage($.trim(newValue) ? "" : overrideMessage || "This field is required");
        }
        validate(target());
        target.subscribe(validate);

        return target;
    };

    ko.extenders.email = function (target, check) {
        target.hasError = ko.observable();

        function validateEmail(emailAddress) {
            target.hasError(!whoever.EmailIsValid(emailAddress));
        };
        validateEmail(target());
        target.subscribe(validateEmail);

        return target;
    };

    ko.extenders.emailRequired = function (target, check) {
        target.hasError = ko.observable();

        function validateEmail(emailAddress) {
            target.hasError(!whoever.EmailIsValid(emailAddress) || whoever.IsEmpty(emailAddress));
        };
        validateEmail(target());
        target.subscribe(validateEmail);

        return target;
    };

    ko.extenders.accountOrEmail = function (target, check) {
        target.hasError = ko.observable();

        function validate(value) {
            if (value)
                if (value.indexOf('@') != -1)
                    target.hasError(!whoever.EmailIsValid(value));
                else
                    target.hasError(whoever.IsEmpty(value));
            else
                target.hasError(true);
        }
        validate(target());
        target.subscribe(validate);

        return target;
    }

    ko.extenders.regExpValidate = function (target, params) {
        //add some sub-observables to our observable
        target.validationMessage = ko.observable();
        if (params.mask() && params.mask() != "") {
            target.hasError = ko.observable(true);
        }
        else {
            target.hasError = ko.observable(false);
        }

        //initial validation
        validate(target());

        //validate whenever the value changes
        target.subscribe(validate);
        //return the original observable
        return target;

        //define a function to do validation
        function validate(newValue) {
            if (params.mask() && params.mask() != "") {
                var regExp = new RegExp(params.mask());
                target.hasError(regExp.test(newValue) ? false : true);
            }
            else {
                target.hasError(false);
            }
        }
    };
}